<template>
  <!-- 添加工作集 -->
  <a-modal
    v-model="show"
    destroyOnClose
    title="添加工作集"
    :maskClosable="false"
    width="50%"
    @cancel="workSetCancel"
  >
    <a-input-search
      v-model="searchValue"
      placeholder="请输入搜索内容"
      style="margin-bottom: 16px"
      @search="onSearch"
    />
    <a-tree
      v-model="checkedKeys"
      checkable
      :tree-data="filteredTreeData"
      :replaceFields="{
        children: 'JobGroupList',
        title: 'JobGroupName',
        key: 'JobGroupID',
      }"
    />
    <template slot="footer">
      <a-button key="back" @click="workSetCancel">取消</a-button>
      <a-button key="submit" type="primary" @click="sumbitWork">提交</a-button>
    </template>
  </a-modal>
</template>

<script>
import { getJobGroupToList } from "@/api/JobRouteOperationRelation";
import {
  insJobProSegmentTypeRelations,
  getJobProSegmentTypeRelationsList,
} from "@/api/ProSegmentType/index";
export default {
  props: ["show", "item"],
  data() {
    return {
      workSetData: [], //所有工作集
      checkedKeys: [], //已关联项
      upLoad: false, //是否全局更新
      searchValue: "", //搜索值
      filteredTreeData: [], //过滤后的树数据
    };
  },
  methods: {
    // 搜索过滤
    onSearch() {
      if (!this.searchValue) {
        this.filteredTreeData = this.workSetData;
        return;
      }

      const filterFn = (node) => {
        if (node.JobGroupName.includes(this.searchValue)) {
          return true;
        }
        if (
          node.JobGroupList &&
          node.JobGroupList.some((child) =>
            child.JobGroupName.includes(this.searchValue)
          )
        ) {
          return true;
        }
        return false;
      };

      this.filteredTreeData = this.workSetData.filter(filterFn).map((node) => {
        if (node.JobGroupList) {
          return {
            ...node,
            JobGroupList: node.JobGroupList.filter((child) =>
              child.JobGroupName.includes(this.searchValue)
            ),
          };
        }
        return node;
      });
    },

    /* 关闭工作集关联弹窗 */
    workSetCancel() {
      this.checkedKeys = []; //清除选中项
      this.$emit("update:show", false);
    },
    //提交关联工作集
    async sumbitWork() {
      let list = [];
      let { ProSegmentTypeID } = this.item;
      this.checkedKeys.map((item) => {
        let obj = {
          ProSegmentTypeID: ProSegmentTypeID,
          jobGroupID: item,
        };
        list.push(obj);
      });
      let res = await insJobProSegmentTypeRelations(list);
      this.$message.success("关联成功");
      this.workSetCancel();
      this.$emit("updata");
    },

    //查询工作集
    async getJobGroupToList() {
      let { RouteOperationID } = this.item;
      let data = {
        RouteOperationID,
      };
      let res = await getJobGroupToList(data);
      this.workSetData = res.Data;
      this.filteredTreeData = res.Data;
      this.workSetData.map((item) => {
        item.JobGroupName = item.JobGroupTypeName;
        item.JobGroupID = item.JobGroupTypeID;
        item.disabled = true;
        item.JobGroupList.map((itm, index) => {
          itm.JobGroupName = itm.JobGroupName + " - " + itm.JobTempletVisionOut;
        });
      });
    },
    //查询已绑定的工作集
    async getJobProSegmentTypeRelationsListFun() {
      let { ProSegmentTypeID } = this.item;
      let data = {
        ProSegmentTypeID,
        pageRequestModel: { pageIndex: 1, pageSize: 100 },
      };
      let { Data } = await getJobProSegmentTypeRelationsList(data);

      let list = [];
      Data.Data.map((item, index) => {
        list.push(item.JobGroupID);
      });
      this.checkedKeys = list;
    },
  },
  watch: {
    show: {
      handler(newValue, oldValue) {
        if (newValue) {
          this.getJobGroupToList();
          this.getJobProSegmentTypeRelationsListFun();
        }
      },
    },
  },
};
</script>

<style></style>